/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: nickhighlighter.cpp 390 2009-05-06 20:18:50Z jpnurmi $
*/

#include "nickhighlighter.h"

QList<QColor> NickHighlighter::colors;

TextBlockUserData::TextBlockUserData() : sender(), color(), index(-1)
{
}

NickHighlighter::NickHighlighter(QTextDocument* parent)
	: QSyntaxHighlighter(parent)
{
    //http://trolltech.com/developer/task-tracker/index_html?method=entry&id=196704
    //for (int h = 0; h < 360; h += 30)
        //colors += QColor::fromHsv(h, saturation, 100);

    if (colors.isEmpty())
    {
        colors << QColor("#BF0000")
            << QColor("#FF0000")
            << QColor("#BF7900")
            << QColor("#00BC0F")
            << QColor("#00A7BA")
            << QColor("#0F00B7")
            << QColor("#8100B5");
    }
}

void NickHighlighter::highlightBlock(const QString& text)
{
    TextBlockUserData* data = dynamic_cast<TextBlockUserData*>(currentBlockUserData());
    if (data && !data->sender.isEmpty())
    {
        if (!data->color.isValid())
            data->color = colors.at(qHash(data->sender) % colors.count());

        if (data->index == -1)
            data->index = text.indexOf(data->sender);

        if (data->index != -1)
        {
            if (data->color.isValid())
                setFormat(data->index, data->sender.length(), data->color);
        }
    }
}
